package com.all.learning.guava.other;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.junit.Test;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Multiset;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;

public class GGset {
	public static void main(String[] args) {
		List<String> historyRemove = Lists.newArrayList("aa","cc");
		List<String> latestRemove = Lists.newArrayList("bb","aa");
		
		
		
		Set<String> newSet = Sets.newHashSet(historyRemove);
		Set<String> oldSet = Sets.newHashSet(latestRemove);
		SetView<String> inter = Sets.intersection(newSet, oldSet);//交集
		List<String> lists =  Lists.newArrayList(inter);
		System.out.println(lists);
	}

	/**
	 * Set交集, 差集, 交集
	 */
	@Test
	public void testSets() {
		HashSet<Integer> newSet = Sets.newHashSet(1, 2, 3, 4, 5);
		HashSet<Integer> oldSet = Sets.newHashSet(5, 3, 1,2,6);
		SetView<Integer> union = Sets.union(newSet, oldSet);
		System.out.println(union);

		SetView<Integer> diff = Sets.difference(newSet, oldSet);//差集
		System.out.println(diff);

		SetView<Integer> inter = Sets.intersection(newSet, oldSet);//交集
		System.out.println(inter);
		List<Integer> lists = Lists.newArrayList(inter);

	}
	
	/**
	 * 不可变集合
	 */
	@Test
	public void testImmutableSet() {
		HashSet<Integer> newSet = Sets.newHashSet(1, 2, 3, 4, 5);
		ImmutableSet.copyOf(newSet);
		ImmutableSet.of("a", "b", "c");
		
		ImmutableSet.Builder<Integer> set = ImmutableSet.builder();
		ImmutableSet<Integer> immuSet = set.addAll(newSet).add(33).build();
		System.out.println(immuSet);
	}
	
	@Test
	public void testMultiset() {
		String[] arr1 = {"aa","aa","bb","cc"};
		Multiset<String> words = HashMultiset.create();
		words.addAll(Arrays.asList(arr1));
		System.out.println(words.count("aa"));
		System.out.println(words.elementSet());
		System.out.println();
		
	}
}
